﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import flash.events.*;
    import Engine.Events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;
    import Engine.*;

    public class WaterManager {

        public static const WATER_COLOR:Number = 0;
        public static const LAND_COLOR:Number = 0xFFFFFF;
        public static const SAND_COLOR:Number = 0xFFFF00;
        public static const RIGHT_SIDE_LAND_COLOR:Number = 0xFF;
        public static const NO_TREE_LAND_COLOR:Number = 0xFF0000;
        public static const RIGHT_SIDE_NO_TREE_LAND_COLOR:Number = 0xFF00FF;
        public static const MINIMUM_VALID_SQUARES_TO_EXPAND:int = 4;

        protected var m_world:GameWorld;
        private var m_loader:Loader = null;
        private var m_collBitmap:Bitmap;
        private var m_item:Item;
        private var m_state:String;
        private var m_collisionBitmap:Bitmap;
        private var m_dbgCollisionShape:Shape = null;

        public function WaterManager(_arg1:GameWorld){
            this.m_world = _arg1;
        }
        public function get showingDbgOverlay():Boolean{
            return (!((this.m_dbgCollisionShape == null)));
        }
        public function showDbgOverlay(_arg1:Boolean):void{
            if (_arg1 == this.showingDbgOverlay){
                return;
            };
            if (_arg1){
                this.dbgCollision();
            } else {
                if (this.m_dbgCollisionShape){
                    GlobalEngine.viewport.objectBase.removeChild(this.m_dbgCollisionShape);
                    this.m_dbgCollisionShape = null;
                };
            };
        }
        public function init():void{
            var _local5:MovieClip;
            var _local6:int;
            var _local7:Bitmap;
            var _local1:XML = Global.gameSettings.getXML();
            var _local2:XMLList = _local1.collisionBackground.layer;
            var _local3:XML = _local2[0];
            if (_local3 == null){
                return;
            };
            this.m_item = Global.gameSettings.getItemByName(_local3.item.@name);
            this.m_state = _local3.item.@state;
            var _local4:ItemImageInstance = this.m_item.getCachedImage(this.m_state);
            if (_local4){
                _local5 = (_local4.image as MovieClip);
                if (_local5){
                    _local6 = 0;
                    while (_local6 < _local5.numChildren) {
                        _local7 = (_local5.getChildAt(_local6) as Bitmap);
                        if (_local7){
                            this.m_collBitmap = _local7;
                        };
                        _local6++;
                    };
                };
                if (((!(this.m_collBitmap)) && ((_local4.image is Bitmap)))){
                    this.m_collBitmap = (_local4.image as Bitmap);
                };
            };
            if (this.m_collBitmap){
                this.onCollisionLoaded(null);
            } else {
                this.m_item.addEventListener(LoaderEvent.LOADED, this.onCollisionLoaded);
            };
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        private function onCollisionLoaded(_arg1:Event):void{
            var _local2:ItemImageInstance;
            var _local3:MovieClip;
            var _local4:int;
            var _local5:Bitmap;
            this.m_item.removeEventListener(LoaderEvent.LOADED, this.onCollisionLoaded);
            if (this.m_collBitmap == null){
                _local2 = this.m_item.getCachedImage(this.m_state);
                if (_local2){
                    _local3 = (_local2.image as MovieClip);
                    if (_local3){
                        _local4 = 0;
                        while (_local4 < _local3.numChildren) {
                            _local5 = (_local3.getChildAt(_local4) as Bitmap);
                            if (_local5){
                                this.m_collBitmap = _local5;
                            };
                            _local4++;
                        };
                    };
                };
            };
            this.setCollision(this.m_collBitmap);
            Global.world.createOverlayBackground();
        }
        public function setCollision(_arg1:Bitmap):void{
            if (_arg1){
                this.m_collisionBitmap = _arg1;
            };
        }
        public function waterDataLoaded():Boolean{
            return (!((this.m_collisionBitmap == null)));
        }
        public function positionOnValidLand(_arg1:Rectangle):Boolean{
            var _local3:int;
            var _local2:int = _arg1.left;
            while (_local2 < (_arg1.left + _arg1.width)) {
                _local3 = _arg1.top;
                while (_local3 < (_arg1.top + _arg1.height)) {
                    if (!this.testValidLand(_local2, _local3)){
                        return (false);
                    };
                    _local3++;
                };
                _local2++;
            };
            return (true);
        }
        public function positionValidForTree(_arg1:Rectangle):Boolean{
            var _local3:int;
            var _local2:int = _arg1.left;
            while (_local2 < (_arg1.left + _arg1.width)) {
                _local3 = _arg1.top;
                while (_local3 < (_arg1.top + _arg1.height)) {
                    if (!this.testValidTree(_local2, _local3)){
                        return (false);
                    };
                    _local3++;
                };
                _local2++;
            };
            return (true);
        }
        public function positionValidForExpansion(_arg1:Rectangle):Boolean{
            var _local5:int;
            var _local2:int;
            var _local3:int = MINIMUM_VALID_SQUARES_TO_EXPAND;
            var _local4:int = _arg1.left;
            while (_local4 < (_arg1.left + _arg1.width)) {
                _local5 = _arg1.top;
                while (_local5 < (_arg1.top + _arg1.height)) {
                    if (this.testValidExpansion(_local4, _local5)){
                        _local2++;
                    };
                    _local5++;
                };
                _local4++;
            };
            return ((_local2 >= _local3));
        }
        public function positionValidPierOnWater(_arg1:Rectangle, _arg2:String, _arg3:int):Boolean{
            var _local8:String;
            var _local9:Array;
            var _local4:int;
            var _local5:int;
            _local4 = _arg1.top;
            while (_local4 < (_arg1.top + _arg1.height)) {
                _local5 = (_arg1.left - _arg3);
                while (_local5 < _arg1.left) {
                    if (!this.testWater(_local5, _local4)){
                        return (false);
                    };
                    _local5++;
                };
                _local4++;
            };
            var _local6:Array = _arg2.split(";");
            var _local7:int;
            while (_local7 < _local6.length) {
                _local8 = _local6[_local7];
                _local9 = _local8.split("|");
                _local5 = (_arg1.right + int(_local9[0]));
                _local4 = (_arg1.top + int(_local9[1]));
                if (!this.testWater(_local5, _local4)){
                    return (true);
                };
                _local7++;
            };
            return (false);
        }
        public function positionValidShipOnWater(_arg1:Rectangle):Boolean{
            var _local3:int;
            var _local2:int = _arg1.left;
            while (_local2 < (_arg1.left + _arg1.width)) {
                _local3 = _arg1.top;
                while (_local3 < (_arg1.top + _arg1.height)) {
                    if (!this.testWater(_local2, _local3)){
                        return (false);
                    };
                    _local3++;
                };
                _local2++;
            };
            return (true);
        }
        public function testValidLand(_arg1:int, _arg2:int):Boolean{
            if (this.m_collisionBitmap == null){
                return (false);
            };
            var _local3:int = this.m_collisionBitmap.bitmapData.rect.width;
            var _local4:int = (_arg1 + (_local3 / 2));
            var _local5:int = (((_local3 / 2) - _arg2) - 1);
            if ((((((((_local4 < 0)) || ((_local4 >= _local3)))) || ((_local5 < 0)))) || ((_local5 >= _local3)))){
                if ((((_local4 < 0)) || ((_local5 >= _local3)))){
                    return (false);
                };
                return (true);
            };
            var _local6:Number = this.m_collisionBitmap.bitmapData.getPixel(_local4, _local5);
            var _local7 = (_local6 == WaterManager.LAND_COLOR);
            if (_local6 == WaterManager.NO_TREE_LAND_COLOR){
                _local7 = true;
            };
            if (Global.player.allowRightSideBuild()){
                if (_local6 == WaterManager.RIGHT_SIDE_LAND_COLOR){
                    _local7 = true;
                };
                if (_local6 == WaterManager.RIGHT_SIDE_NO_TREE_LAND_COLOR){
                    _local7 = true;
                };
            };
            return (_local7);
        }
        public function testValidExpansion(_arg1:int, _arg2:int):Boolean{
            if (this.m_collisionBitmap == null){
                return (false);
            };
            var _local3:int = this.m_collisionBitmap.bitmapData.rect.width;
            var _local4:int = (_arg1 + (_local3 / 2));
            var _local5:int = (((_local3 / 2) - _arg2) - 1);
            if ((((((((_local4 < 0)) || ((_local4 >= _local3)))) || ((_local5 < 0)))) || ((_local5 >= _local3)))){
                if ((((_local4 < 0)) || ((_local5 >= _local3)))){
                    return (false);
                };
                return (true);
            };
            var _local6:Number = this.m_collisionBitmap.bitmapData.getPixel(_local4, _local5);
            var _local7 = (_local6 == WaterManager.LAND_COLOR);
            if (_local6 == WaterManager.NO_TREE_LAND_COLOR){
                _local7 = true;
            };
            if (Global.player.allowRightSideBuild()){
                if (_local6 == WaterManager.RIGHT_SIDE_LAND_COLOR){
                    _local7 = true;
                };
                if (_local6 == WaterManager.RIGHT_SIDE_NO_TREE_LAND_COLOR){
                    _local7 = true;
                };
            };
            return (_local7);
        }
        public function testValidTree(_arg1:int, _arg2:int):Boolean{
            if (this.m_collisionBitmap == null){
                return (false);
            };
            var _local3:int = this.m_collisionBitmap.bitmapData.rect.width;
            var _local4:int = (_arg1 + (_local3 / 2));
            var _local5:int = (((_local3 / 2) - _arg2) - 1);
            if ((((((((_local4 < 0)) || ((_local4 >= _local3)))) || ((_local5 < 0)))) || ((_local5 >= _local3)))){
                if ((((_local4 < 0)) || ((_local5 >= _local3)))){
                    return (false);
                };
                return (true);
            };
            var _local6:Number = this.m_collisionBitmap.bitmapData.getPixel(_local4, _local5);
            var _local7 = (_local6 == WaterManager.LAND_COLOR);
            if (_local6 == WaterManager.RIGHT_SIDE_LAND_COLOR){
                _local7 = true;
            };
            if (_local6 == WaterManager.NO_TREE_LAND_COLOR){
                _local7 = false;
            };
            return (_local7);
        }
        public function testWater(_arg1:int, _arg2:int):Boolean{
            if (this.m_collisionBitmap == null){
                return (false);
            };
            var _local3:int = this.m_collisionBitmap.bitmapData.rect.width;
            var _local4:int = (_arg1 + (_local3 / 2));
            var _local5:int = (((_local3 / 2) - _arg2) - 1);
            if ((((((((_local4 < 0)) || ((_local4 >= _local3)))) || ((_local5 < 0)))) || ((_local5 >= _local3)))){
                if ((((_local4 < 0)) || ((_local5 >= _local3)))){
                    return (true);
                };
                return (false);
            };
            var _local6:Number = this.m_collisionBitmap.bitmapData.getPixel(_local4, _local5);
            var _local7 = (_local6 == WATER_COLOR);
            return (_local7);
        }
        public function testSand(_arg1:int, _arg2:int):Boolean{
            if (this.m_collisionBitmap == null){
                return (false);
            };
            var _local3:int = this.m_collisionBitmap.bitmapData.rect.width;
            var _local4:int = (_arg1 + (_local3 / 2));
            var _local5:int = (((_local3 / 2) - _arg2) - 1);
            if ((((((((_local4 < 0)) || ((_local4 >= _local3)))) || ((_local5 < 0)))) || ((_local5 >= _local3)))){
                return (false);
            };
            var _local6:Number = this.m_collisionBitmap.bitmapData.getPixel(_local4, _local5);
            var _local7 = (_local6 == SAND_COLOR);
            return (_local7);
        }
        private function dbgCollision():void{
            var _local1:int;
            var _local2:int;
            var _local3:Boolean;
            var _local4:Point;
            this.m_dbgCollisionShape = new Shape();
            GlobalEngine.viewport.objectBase.addChild(this.m_dbgCollisionShape);
            _local1 = -72;
            while (_local1 < 72) {
                _local2 = -72;
                while (_local2 < 72) {
                    _local3 = this.testWater(_local1, _local2);
                    this.m_dbgCollisionShape.graphics.lineStyle(0.5, 4278255615);
                    if ((((((_local1 == -72)) || ((_local1 == 71)))) && ((((_local2 == -72)) || ((_local2 == 71)))))){
                        _local3 = true;
                        this.m_dbgCollisionShape.graphics.lineStyle(0.5, 4294901760);
                    };
                    _local4 = IsoMath.tilePosToPixelPos(_local1, _local2);
                    this.m_dbgCollisionShape.graphics.moveTo(_local4.x, _local4.y);
                    _local4 = IsoMath.tilePosToPixelPos((_local1 + 1), _local2);
                    this.m_dbgCollisionShape.graphics.lineTo(_local4.x, _local4.y);
                    _local4 = IsoMath.tilePosToPixelPos((_local1 + 1), (_local2 + 1));
                    this.m_dbgCollisionShape.graphics.lineTo(_local4.x, _local4.y);
                    _local4 = IsoMath.tilePosToPixelPos(_local1, (_local2 + 1));
                    this.m_dbgCollisionShape.graphics.lineTo(_local4.x, _local4.y);
                    _local4 = IsoMath.tilePosToPixelPos(_local1, _local2);
                    this.m_dbgCollisionShape.graphics.lineTo(_local4.x, _local4.y);
                    _local2++;
                };
                _local1++;
            };
        }
        protected function doGenerateWater():void{
            this.dbgCollision();
        }
        public function positionValidOnLandAndWater(_arg1:Rectangle, _arg2:int=1, _arg3:int=1):Boolean{
            var _local7:int;
            var _local4:int;
            var _local5:int;
            var _local6:int = _arg1.left;
            while (_local6 < (_arg1.left + _arg1.width)) {
                _local7 = _arg1.top;
                while (_local7 < (_arg1.top + _arg1.height)) {
                    if (this.testValidLand(_local6, _local7)){
                        _local4++;
                    };
                    if (this.testWater(_local6, _local7)){
                        _local5++;
                    };
                    if ((((_local5 > _arg3)) && ((_local4 > _arg2)))){
                        return (true);
                    };
                    _local7++;
                };
                _local6++;
            };
            return (false);
        }

    }
}//package Classes.sim 
